﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2015-1-17
 * 时间: 15:01
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SQLite;
using System.Data.Common;
using System.IO;
using sqlliteop;

namespace voice
{
	/// <summary>
	/// Description of set.
	/// </summary>
	public partial class set : Form
	{
		public set()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			comboBox1.Items.Clear();
			string sql="select * from sqlite_master where type='table' and name is not 'sqlite_sequence'";
			comboBox1.DataSource=sqlliteop.SQLiteHelper.Query(sql).Tables[0];
			comboBox1.DisplayMember="name";
			comboBox1.ValueMember="name";
			toolStripStatusLabel1.Text="当前方案：";
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		void Button4Click(object sender, EventArgs e)
		{
			OpenFileDialog open1 = new OpenFileDialog();
			open1.Filter = "所有MP3文件|*.MP3";
			open1.InitialDirectory= System.Environment.CurrentDirectory + @"/audio/";
			open1.ShowDialog();//打开这个窗口
			if (open1.ShowDialog()==DialogResult.OK)
			{
				textBox2.Text=Path.GetFileName(open1.FileName.ToString());
			}
		}
		void Button3Click(object sender, EventArgs e)
		{
			if  (File.Exists(System.Environment.CurrentDirectory + @"/data/" +"commad.db3"))
			{
			}
			else
			{
				string databaseFileName = System.Environment.CurrentDirectory + @"/data/" +"commad.db3";
				SQLiteConnection.CreateFile(databaseFileName);
			};
			//判断方案是否已经存在,不存在就创建
			if (textBox1.Text==string.Empty)
			{
				textBox1.Focus();
			}
			else
			{
				
				SQLiteHelper.creatTabel(textBox1.Text);
				//选中当前方案
				toolStripStatusLabel1.Text="当前方案："+textBox1.Text;
				string sql="select * from ["+textBox1.Text+"]";
				dataGridView1.DataSource=sqlliteop.SQLiteHelper.Query(sql).Tables[0];
			}
			
			
		}
		void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
		{
			toolStripStatusLabel1.Text="当前方案："+comboBox1.Text;
			string sql="select * from ["+comboBox1.Text+"]";
			dataGridView1.DataSource=sqlliteop.SQLiteHelper.Query(sql).Tables[0];
		}
		
		//取得当前方案名称
		string gettablename()
		{
			string s=toolStripStatusLabel1.Text.Substring(5);
			return s;
			
		}
		//刷新datagridview
		void datagridviewreload()
		{
			string sql="select * from ["+ gettablename() +"]";
			dataGridView1.DataSource=sqlliteop.SQLiteHelper.Query(sql).Tables[0];
		}
		
		//录入指令
		void Button1Click(object sender, EventArgs e)
		{
			//dateTimePicker1.Format=System.Windows.Forms.DateTimePickerFormat.Custom;
            //dateTimePicker1.CustomFormat = "yyyy-MM-dd HH:mm:ss";
			string sql=string.Empty;
			if (gettablename()==string.Empty)
			{
				MessageBox.Show("播放方案不能为空，请重新创建或选择已有方案！","警告",MessageBoxButtons.OK);
				comboBox1.Focus();
			}
			else
			{
				switch(tabControl1.SelectedIndex)
				{
					case 0:
						if (TextBoxzn.Text==string.Empty)
						{
							MessageBox.Show("文本指令不能为空，请重新输入！","警告",MessageBoxButtons.OK);
							TextBoxzn.Focus();
							break;
						}
						else
						{
							sql="insert into ["+gettablename()+"] ([typeID],[runtime],[cmd]) values (1,'"+dateTimePicker1.Text+"','"+TextBoxzn.Text+"')";
							break;
						};
						
					case 1:
						if (textBox2.Text==string.Empty)
						{
							MessageBox.Show("语音指令不能为空，请重新输入！","警告",MessageBoxButtons.OK);
							textBox2.Focus();
							break;
						}
						else
						{
							sql="insert into ["+gettablename()+"]([typeID],[runtime],[cmd]) values(2,'"+dateTimePicker1.Text+"','"+textBox2.Text+"')";
							break;
						};
						
				}
				//TextBoxzn.Text=sql;
				sqlliteop.SQLiteHelper.ExecuteSql(sql);
				datagridviewreload();
			}
		}
		//删除指令
		void Button2Click(object sender, EventArgs e)
		{
			if (textBox3.Text==string.Empty)
			{
				MessageBox.Show("要删除的序号不能为空，请重新输入！","警告",MessageBoxButtons.OK);
				textBox3.Focus();
			}
			else
			{
				string sql="delete from ["+gettablename()+"] where _id="+Convert.ToInt32(textBox3.Text);
				sqlliteop.SQLiteHelper.ExecuteSql(sql);
				datagridviewreload();
				
			};
		}
		//清空方案中的指令
		void Button7Click(object sender, EventArgs e)
		{
			if (gettablename()==string.Empty)
			{
				MessageBox.Show("播放方案不能为空，请重新创建或选择已有方案！","警告",MessageBoxButtons.OK);
				comboBox1.Focus();
			}
			else
			{
				string sql="delete from ["+gettablename()+"]";
				sqlliteop.SQLiteHelper.ExecuteSql(sql);
				//清除自增序号
				sql="UPDATE sqlite_sequence SET seq = 0 WHERE name = "+"'"+gettablename()+"'";
				sqlliteop.SQLiteHelper.ExecuteSql(sql);
				datagridviewreload();
			}
		}
		
		//修改指令
		void Button6Click(object sender, EventArgs e)
		{
			string sql=string.Empty;
			if (gettablename()==string.Empty)
			{
				MessageBox.Show("播放方案不能为空，请重新创建或选择已有方案！","警告",MessageBoxButtons.OK);
				comboBox1.Focus();
			}
			else
			{
				switch(tabControl1.SelectedIndex)
				{
					case 0:
						if (TextBoxzn.Text==string.Empty)
						{
							MessageBox.Show("文本指令不能为空，请重新输入！","警告",MessageBoxButtons.OK);
							TextBoxzn.Focus();
							break;
						}
						else
						{
							sql="update  ["+gettablename()+"] set [typeID]=1,[runtime]='"+Convert.ToDateTime(dateTimePicker1.Text)+"',[cmd]='"+TextBoxzn.Text+"'where _id="+Convert.ToInt32(textBox3.Text);
							break;
						};
						
					case 1:
						if (textBox2.Text==string.Empty)
						{
							MessageBox.Show("语音指令不能为空，请重新输入！","警告",MessageBoxButtons.OK);
							textBox2.Focus();
							break;
						}
						else
						{
							sql="update  ["+gettablename()+"] set [typeID]=2,[runtime]='"+Convert.ToDateTime(dateTimePicker1.Text)+"',[cmd]='"+textBox2.Text+" 'where _id="+Convert.ToInt32(textBox3.Text);
							break;
						};
						
				}
				//TextBoxzn.Text=sql;
				sqlliteop.SQLiteHelper.ExecuteSql(sql);
				datagridviewreload();
			}
		}
		
		//选中列表项自动赋值
		void DataGridView1MouseClick(object sender, MouseEventArgs e)
		{
			if (dataGridView1.SelectedRows.Count>0)
			{
				textBox3.Text=dataGridView1.CurrentRow.Cells[0].Value.ToString();
				//dateTimePicker1.Text=dataGridView1.CurrentRow.Cells[1].Value.ToString();
				switch (Convert.ToInt32(dataGridView1.CurrentRow.Cells[1].Value))
				{
					case 1:
						{
							TextBoxzn.Text=dataGridView1.CurrentRow.Cells[3].Value.ToString();
							tabControl1.SelectedIndex=0;
							break;
						}
					case 2:
						{
							textBox2.Text=dataGridView1.CurrentRow.Cells[3].Value.ToString();
							tabControl1.SelectedIndex=1;
							break;
						}
				}
				
				
			}
			
		}
		
	}
}
